package com.zhiqianbao.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhiqianbao.R;
import com.zhiqianbao.entity.CustomerServiceRecord;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by liang on 2016/2/18.
 */
public class MessageListAdapter extends BaseAdapter {

    List<CustomerServiceRecord> list;
    private LayoutInflater inflater;
    private Activity ctx;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public MessageListAdapter(Activity context, List<CustomerServiceRecord> list) {
        this.ctx = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    private final class ListItemView {
        /**
         * 我发送消息的时间
         */
        TextView tvTimeMy;
        /**
         * 我发送的消息内容
         */
        TextView tvMsgMy;
        /**
         * 客服发送的消息时间
         */
        TextView tvTime;
        /**
         * 客服发送的消息内容
         */
        TextView tvMsg;
        /**
         * 自己发的消息
         */
        LinearLayout llyMy;
        /**
         * 客服发的消息
         */
        LinearLayout lly;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ListItemView listItemView = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.msg_item_list, null);
            listItemView = new ListItemView();

            listItemView.tvTimeMy = (TextView) convertView.findViewById(R.id.tv_time_my);
            listItemView.tvMsgMy = (TextView) convertView.findViewById(R.id.tv_msg_my);
            listItemView.tvMsg = (TextView) convertView.findViewById(R.id.tv_msg);
            listItemView.tvTime = (TextView) convertView.findViewById(R.id.tv_time);

            listItemView.llyMy = (LinearLayout) convertView.findViewById(R.id.lly_my);
            listItemView.lly = (LinearLayout) convertView.findViewById(R.id.lly);

            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }

        CustomerServiceRecord customerServiceRecord = list.get(position);
        String time =  sdf.format(new Date(customerServiceRecord.getCreateTime()));
        String message = customerServiceRecord.getContent();

        //如果消息是用户发的
        if (customerServiceRecord.getType().equals("0")) {
            listItemView.llyMy.setVisibility(View.VISIBLE);
            listItemView.lly.setVisibility(View.GONE);
            listItemView.tvTimeMy.setText(time);
            listItemView.tvMsgMy.setText(message);
        //如果消息是客服发的
        } else {
            listItemView.llyMy.setVisibility(View.GONE);
            listItemView.lly.setVisibility(View.VISIBLE);
            listItemView.tvTime.setText(time);
            listItemView.tvMsg.setText(message);
        }

        return convertView;
    }
}
